package com.miot.im.util;

import java.math.BigInteger;
import java.util.Map;

public class DecodeUtil {
    /**
     * byte数组转hex
     * @param bytes
     * @return
     */
    public static String byteToHex(byte[] bytes){
        String strHex = "";
        StringBuilder sb = new StringBuilder("");
        for (int n = 0; n < bytes.length; n++) {
            strHex = Integer.toHexString(bytes[n] & 0xFF);
            sb.append((strHex.length() == 1) ? "0" + strHex : strHex); // 每个字节由两个字符表示，位数不够，高位补0
        }
        return sb.toString().trim();
    }

    /**
     * hex转byte数组
     * @param hex
     * @return
     */
    public static byte[] hexToByte(String hex){
        int m = 0, n = 0;
        int byteLen = hex.length() / 2; // 每两个字符描述一个字节
        byte[] ret = new byte[byteLen];
        for (int i = 0; i < byteLen; i++) {
            m = i * 2 + 1;
            n = m + 1;
            int intVal = Integer.decode("0x" + hex.substring(i * 2, m) + hex.substring(m, n));
            ret[i] = Byte.valueOf((byte)intVal);
        }
        return ret;
    }

    public static  String[] hexToDec(String hex) {

        String[] returnArray = new String[17];
        String hexStr = hex;
        String[] hexByteArray= hexStr.split(" ");

        //获取数据长度
        String datalength = HexToIntegerStr(hexByteArray[3]);

        String devStatu = HexToIntegerStr(hexByteArray[4]);  //开机状态
        returnArray[0] = devStatu;
        String workType = HexToIntegerStr(hexByteArray[5]); //工作模式
        returnArray[1] = workType;
        String sSpeed = HexToIntegerStr(hexByteArray[6]); //送风机风速
        returnArray[2] = sSpeed;
        String pSpeed = HexToIntegerStr(hexByteArray[7]); //排风机风速
        returnArray[3] = pSpeed;
        String hotStatu = HexToIntegerStr(hexByteArray[8]); //热交换和旁通阀状态
        returnArray[4] = hotStatu;
        String cleanStatu = HexToIntegerStr(hexByteArray[9]); //净化设备状态
        returnArray[5] = cleanStatu;
        String flzStatu = HexToIntegerStr(hexByteArray[10]); //负离子状态
        returnArray[6] = flzStatu;
        String zwdStatu = HexToIntegerStr(hexByteArray[11]); //紫外灯状态
        returnArray[7] = zwdStatu;
        String wjqStatu = HexToIntegerStr(hexByteArray[12]); //加热器状态
        returnArray[8] = wjqStatu;
        String csjStatu = HexToIntegerStr(hexByteArray[13]); //除湿机状态
        returnArray[9] = csjStatu;
        Long wdData = HexToLongDecimal(hexByteArray[14]); //温度数据（℃）
        returnArray[10] = wdData+"";
        Long sdData = HexToLongDecimal(hexByteArray[15]); //湿度数据
        returnArray[11] = sdData+"";
        Long pm25Date = HexToLongDecimal(hexByteArray[16]+hexByteArray[17]); //pm2.5浓度数
        returnArray[12] = pm25Date+"";
        Long co2Data = HexToLongDecimal(hexByteArray[18]+hexByteArray[19]); //二氧化碳浓
        returnArray[13] = co2Data+"";
        Long vocData = HexToLongDecimal(hexByteArray[20]+hexByteArray[21]); //VOC浓度数
        returnArray[14] = vocData+"";
        String lwsyData = HexToIntegerStr(hexByteArray[22]); //滤网剩余比例
        returnArray[15] = lwsyData;
        String jqStatu = HexToIntegerStr(hexByteArray[23]); //机器锁定（后台）
        returnArray[16] = jqStatu;

        return  returnArray;
    }


    public static Long HexToLongDecimal(String s) {
        return Long.parseLong(s, 16);
    }

    public static BigInteger HexToBigIntDecimal(String s) {
        return new BigInteger(s, 16);
    }

    public static Integer HexToInteger(String s) {
        return Integer.parseInt(s,16);
    }

    public static String HexToIntegerStr(String s) {
        String str = "";
        if(null != s) {
            s = Integer.parseInt(s,16) + "";
        }
        return s;
    }

    public static String LongDecimalToHex(Long l) {
        return Long.toHexString(l);
    }

    public static String BigIntDecimalToHex(String s) {
        return new BigInteger(s, 10).toString(10);
    }
}
